//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.bstek.ureport.console;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cup.core.log.exception.ServiceException;
import cup.core.secure.CupUser;
import cup.core.secure.utils.AuthUtil;
import cup.core.tool.api.ResultCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class UReportServlet extends HttpServlet {
	private static final long serialVersionUID = 533049461276487971L;
	public static final String URL = "/ureport";
	private Map<String, ServletAction> actionMap = new HashMap();

	public UReportServlet() {
	}

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		WebApplicationContext applicationContext = this.getWebApplicationContext(config);
		Collection<ServletAction> handlers = applicationContext.getBeansOfType(ServletAction.class).values();
		Iterator var4 = handlers.iterator();

		while (var4.hasNext()) {
			ServletAction handler = (ServletAction) var4.next();
			String url = handler.url();
			if (this.actionMap.containsKey(url)) {
				throw new RuntimeException("Handler [" + url + "] already exist.");
			}

			this.actionMap.put(url, handler);
		}

	}

	protected WebApplicationContext getWebApplicationContext(ServletConfig config) {
		return WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
	}

	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String path = req.getContextPath() + "/ureport";
		String uri = req.getRequestURI();
//		if (uri.equals("/ureport/designer")) {
//			CupUser user = AuthUtil.getUser();
//			if (null == user) {
//				throw new ServiceException(ResultCode.UN_USER_NOT_AUTHORIZED);
//			}
//		}
		String targetUrl = uri.substring(path.length());
		if (targetUrl.length() < 1) {
			this.outContent(resp, "Welcome to use ureport,please specify target url.");
		} else {
			int slashPos = targetUrl.indexOf("/", 1);
			if (slashPos > -1) {
				targetUrl = targetUrl.substring(0, slashPos);
			}

			ServletAction targetHandler = (ServletAction) this.actionMap.get(targetUrl);
			if (targetHandler == null) {
				this.outContent(resp, "Handler [" + targetUrl + "] not exist.");
			} else {
				RequestHolder.setRequest(req);

				try {
					targetHandler.execute(req, resp);
				} catch (Exception var15) {
					resp.setCharacterEncoding("UTF-8");
					PrintWriter pw = resp.getWriter();
					Throwable e = this.buildRootException(var15);
					resp.setStatus(500);
					String errorMsg = e.getMessage();
					if (StringUtils.isBlank(errorMsg)) {
						errorMsg = e.getClass().getName();
					}

					pw.write(errorMsg);
					pw.close();
					throw new ServletException(var15);
				} finally {
					RequestHolder.clean();
				}

			}
		}
	}

	private Throwable buildRootException(Throwable throwable) {
		return throwable.getCause() == null ? throwable : this.buildRootException(throwable.getCause());
	}

	private void outContent(HttpServletResponse resp, String msg) throws IOException {
		resp.setContentType("text/html");
		PrintWriter pw = resp.getWriter();
		pw.write("<html>");
		pw.write("<header><title>UReport Console</title></header>");
		pw.write("<body>");
		pw.write(msg);
		pw.write("</body>");
		pw.write("</html>");
		pw.flush();
		pw.close();
	}
}
